﻿
#include "Game.h"




//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

_GAME::_GAME(QObject *parent)
    :QThread(parent)
{
    int i;

    wordLib = new WordLib;

    m_PaiDui = new _Pai[GAME_MAX_NUM_PAI];
    m_WuJiang = new _WuJiang[GAME_MAX_NUM_WUJIANG];
    m_Player = new _Player[GAME_MAX_NUM_PLAYER];		//玩家


    for(i = 0; i < GAME_MAX_NUM_PLAYER; i++)
    {
        m_Player[i].setProp(i);
        m_PlayerList.append(m_Player + i);
    }

    for(i = 0; i < GAME_MAX_NUM_PAI; i++)
    {
        //m_PaiDui[i].iID = i;
        m_PaiDuiList.append(m_PaiDui + i);
    }
    for(i = 0; i < GAME_MAX_NUM_WUJIANG; i++)
    {
        //m_WuJiang[i].iID = i;
        m_WuJiangList.append(m_WuJiang + i);
    }


    int n = 0;
    i = 0;
    for(n += 20; i < n; i++)  //杀
    {
        m_PaiDui[i].setProp(i,_Pai::Type_JiBen,_Pai::Value_Sha);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Sha);
    }
    for(n += 10; i < n; i++)  //闪
    {
        m_PaiDui[i].setProp(i,_Pai::Type_JiBen,_Pai::Value_Shan);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Shan);
    }
    for(n += 6; i < n; i++) //桃
    {
        m_PaiDui[i].setProp(i,_Pai::Type_JiBen,_Pai::Value_Tao);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Tao);
    }
    for(n += 4; i < n; i++)   //拆
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_Chai);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Chai);
    }
    for(n += 3; i < n; i++)   //顺
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_Shun);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Shun);
    }
    for(n += 3; i < n; i++)   //无中
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_WuZhong);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_WuZhong);
    }
    for(n += 2; i < n; i++)   //借刀
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_JieDao);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_JieDao);
    }
    for(n += 1; i < n; i++)   //万剑
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_WanJian);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_WanJian);
    }
    for(n += 2; i < n; i++)   //南蛮
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_NanMan);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_NanMan);
    }
    for(n += 8; i < n; i++)   //高考
    {
        m_PaiDui[i].setProp(i,_Pai::Type_FeiYanChiJingNang,_Pai::Value_GaoKao);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_GaoKao);
    }
    for(n += 2; i < n; i++)   //闪电
    {
        m_PaiDui[i].setProp(i,_Pai::Type_YanChiJingNang,_Pai::Value_ShanDian);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_ShanDian);
    }
    for(n += 3; i < n; i++)   //乐
    {
        m_PaiDui[i].setProp(i,_Pai::Type_YanChiJingNang,_Pai::Value_LeBuSiShu);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_Le);
    }
    for(n += 2; i < n; i++)   //连弩
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_LianNu);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_LianNu);
    }
    for(n += 1; i < n; i++)   //丈八
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_ZhangBa);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_ZhangBa);
    }
    for(n += 1; i < n; i++)   //青龙
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_QingLong);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_QingLong);
    }
    for(n += 1; i < n; i++)   //贯石斧
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_GuanShiFu);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_GuanShiFu);
    }
    for(n += 1;i < n; i++)   //寒冰
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_HanBing);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_HanBingJian);
    }
    for(n += 2; i < n; i++)   //碳素笔
    {
        m_PaiDui[i].setProp(i,_Pai::Type_WuQi,_Pai::Value_TanSu);
        //m_PaiDui[i].setPixmap(GraphicsButton::Normal,pGameRes->pai_TanSu);
    }


    //!!加武将!!
    for(i = 0;i < GAME_MAX_NUM_WUJIANG; i++)   //碳素笔
    {
        m_WuJiang[i].setProp(i,4);
    }

    msgHash[_Pai::Value_Sha].append(_T("请选择一名角色，作为%1的目标"));
    msgHash[_Pai::Value_Sha].append(_T("请出一张%1"));
    msgHash[_Pai::Value_Sha].append(_T("%1对%2使用%3"));
    msgHash[_Pai::Value_Sha].append(_T("！%1不出%2！"));
    msgHash[_Pai::Value_Sha].append(_T("！%1出%2！"));

    msgHash[_Pai::Value_Shan].append(_T("请出一张%1"));
    msgHash[_Pai::Value_Shan].append(_T("！%1不出%2！"));
    msgHash[_Pai::Value_Shan].append(_T("！%1出%2！"));

    msgHash[_Pai::Value_Tao].append(_T("请出一张%1"));
    msgHash[_Pai::Value_Tao].append(_T("%1使用%2"));
    msgHash[_Pai::Value_Tao].append(_T("！%1濒死，是否使用%2？"));
    msgHash[_Pai::Value_Tao].append(_T("！%1不出%2!"));
    msgHash[_Pai::Value_Tao].append(_T("！%1出%2!"));

    msgHash[_Pai::Value_Shun].append(_T("使用%1，请选择一名角色"));
    msgHash[_Pai::Value_Shun].append(_T("%1，请选一张牌"));
    msgHash[_Pai::Value_Shun].append(_T("%1对%2使用%3"));
    msgHash[_Pai::Value_Shun].append(_T("！%1顺走%2的牌%3"));

    msgHash[_Pai::Value_Chai].append(_T("使用%1，请选择一名角色"));
    msgHash[_Pai::Value_Chai].append(_T("%1，请选一张牌"));
    msgHash[_Pai::Value_Chai].append(_T("%1对%2使用%3"));
    msgHash[_Pai::Value_Chai].append(_T("！%1拆掉%2的%3"));

    msgHash[_Pai::Value_WuZhong].append(_T("%1使用%2"));
    msgHash[_Pai::Value_WuZhong].append(_T("%1使用%2！"));

    msgHash[_Pai::Value_JieDao].append(_T("使用%1，请选择要借笔的目标"));
    msgHash[_Pai::Value_JieDao].append(_T("请选择被%1的目标"));
    msgHash[_Pai::Value_JieDao].append(_T("%1对%2使用%3，目标为%4"));
    msgHash[_Pai::Value_JieDao].append(_T("！%1借%2杀%3！"));
    msgHash[_Pai::Value_JieDao].append(_T("！%1得到%2的%3！"));

    msgHash[_Pai::Value_GaoKao].append(_T("使用%1，请选择一名角色"));
    msgHash[_Pai::Value_GaoKao].append(_T("%1对%2使用%3"));

    msgHash[_Pai::Value_WanJian].append(_T("%1使用%2"));

    msgHash[_Pai::Value_NanMan].append(_T("%1使用%2"));

    msgHash[_Pai::Value_LeBuSiShu].append(_T("使用%1，请选择一名角色"));
    //msgHash[_Pai::Value_LeBuSiShu].append(_T("%1判定结果%2，判定成功，"));
    msgHash[_Pai::Value_LeBuSiShu].append(_T("%1对%2使用%3"));
    msgHash[_Pai::Value_LeBuSiShu].append(_T("%1发动中"));

    msgHash[_Pai::Value_ShanDian].append(_T("%1开始判定……"));
    msgHash[_Pai::Value_ShanDian].append(_T("%1判定结果%2，判定成功"));
    //msgHash[_Pai::Value_ShanDian].append(_T("回答错误,%1掉血1滴!"));
    msgHash[_Pai::Value_ShanDian].append(_T("%1判定结果%2，判定失败"));
    msgHash[_Pai::Value_ShanDian].append(_T("%1使用%2"));


    msgHash[_Pai::Value_LianNu].append(_T("%1装备%2"));

    msgHash[_Pai::Value_GuanShiFu].append(_T("您可以选择两张手牌弃掉，继续造成一点伤害"));
    msgHash[_Pai::Value_GuanShiFu].append(_T("！%1发动%2！"));

    msgHash[_Pai::Value_HanBing].append(_T("选择确定可以弃掉对方任意两张手牌，取消则继续造成伤害"));
    msgHash[_Pai::Value_HanBing].append(_T("！%1发动%2！"));

    msgHash[_Pai::Value_QingLong].append(_T("！%1效果出发"));

    msgHash[_Pai::Value_ZhangBa].append(_T("请选择任意两种手牌当%1使用"));

    msgHash[_Pai::Value_TanSu].append(_T("请选择两张%1使用"));
    msgHash[_Pai::Value_TanSu].append(_T("！%1的%2效果出发！"));

    msgHash[_Pai::Value_All].append(_T("出牌阶段，请选择一张卡牌"));
    msgHash[_Pai::Value_All].append(_T("您需要弃掉%1张卡牌，请选牌"));
    msgHash[_Pai::Value_All].append(_T("%1摸%2张牌"));
    msgHash[_Pai::Value_All].append(_T("%1掉%2血！"));
    msgHash[_Pai::Value_All].append(_T("%1死亡"));
    msgHash[_Pai::Value_All].append(_T("%1弃牌:%2"));
    //msgHash[_Pai::Value_All].append(_T("%1回答错误"));
    //msgHash[_Pai::Value_All].append(_T("%1回答正确!"));
    //msgHash[_Pai::Value_Sha].append();
}

_GAME::~_GAME()
{
    if(wordLib != NULL)
    {

        delete[] m_PaiDui;
        m_PaiDui = NULL;
        delete[] m_WuJiang;
        m_WuJiang = NULL;
        delete[] m_Player;
        m_Player = NULL;
        delete wordLib;
        wordLib = NULL;
    }
}
int _GAME::Init()
{
    emit s_GameInit(playerCount,iMyID);
    Debug(QString("随机数种子:%1").arg(srand()));
    iMainTurn = 0;
	
	memset(&dianjipai,0,sizeof(dianjipai));
    memset(step,0,sizeof(step));
    shortMsg = "";
    memset(PaiDuiID,0,sizeof(PaiDuiID));


    FlagMenus(BASE_PAI::Flag_Null,BASE_PAI::Flag_Null,BASE_PAI::Flag_Null);

    wordLib->deleteAll();
    eWinner = Game_ShenFen_NULL;
    eGameStatus = Game_Status_Continue;
    answer = false;


    int i;
    for(i = 0; i < GAME_MAX_NUM_PAI; i++)	//清除状态
    {
        m_PaiDui[i].init();
    }
    for(i = 0; i < GAME_MAX_NUM_WUJIANG; i++)	//清除已用武将和状态
    {
        m_WuJiang[i].init();
    }
    for(i = 0; i < playerCount; i++)	//清除武将数
    {
        m_Player[i].init();
        m_Player[i].bUsing = true;
    }
    for(; i < GAME_MAX_NUM_PLAYER; i++)	//清除武将数
    {
        m_Player[i].init();
    }
    //pGameRes->init();


    QiPaiDuiShu = GAME_MAX_NUM_PAI;
    PaiDuiShu = 0;
    for(i = 0; i < GAME_MAX_NUM_PAI; i++)	//
    {
        QiPaiDui[i] = m_PaiDui + i;
        //PaiDui[i]->iFlag = BASE_PAI::flag_Show;
        //PaiDui[i]->setPosition(BASE_PAI::Area_PaiDui,i,-1);
    }
    Xipai();

    //!!!分配ID!!!
    //iMyID = rand() % playerCount;
    //for(i = 0; i < jiaoSeList.count(); i++)
    //    m_Player[i].eJiaoSe = jiaoSeList.at(i);

    setGameStatus(Game_Status_Continue);

    ChoiceWuJiang(playerCount);
    emit s_RefreshAllPokePosition();
	
	//Player[iMyID].Kanshoupai();
	
	//cstr.Format(_T("牌堆还剩%d张牌！"),paiduishu);
	//ShowMessage(cstr);

    for(i = 0; i < playerCount; i++)
    {
        Debug(tr("Player%1'JiaoSe is %2").arg(i).arg(m_Player[i].eJiaoSe));
    }
	return 0;
	
}

int _GAME::ChoiceWuJiang(int count)
{
    //选择武将
    ////!!!!scanf("%d",&n);
    ///
    //

    ShowMessage(_T("选择武将!"));

    int i;
    long *temp = new long[count];
    diffarr(0,GAME_MAX_NUM_WUJIANG,temp,count);

    for(i = 0; i < count; i++)
    {
        m_WuJiang[temp[i]].bUsing = true;
        m_Player[i].getWuJiang(m_WuJiang + temp[i]);
        m_Player[i].eJiaoSe = _Player::JiaoSe_AI;
    }

    delete temp;

    m_Player[iMyID].eJiaoSe = _Player::JiaoSe_Self;




    return 0;
}

_GAME::_GameStatus _GAME::GameBegin()
{
    ShowMessage(_T("<font size=10>---英语杀 By Pleafles---</font>"));
    ShowMessage(_T("GameBegin!"));
    ShowChuPaiMessage(_T("游戏开始中..."));

    for(int i = 0; i < GAME_MAX_NUM_PAI; i++)	//清除状态
    {
        //m_PaiDui[i].setFlag(BASE_PAI::flag_Show);
        m_PaiDui[i].bUsing = true;
    }
    //Sleep(5000);

    Sleep(config.configHash["DELAY_TIME_COMPUTER"].toInt());

	for(int i = iMainTurn;;)
	{
		MoPai(i,4);
		i++;
		i = i % playerCount;
		if(i == iMainTurn)
			break;
    }
    emit s_RefreshAllPokePosition();
    emit s_RefreshAllFlags();

    Debug(_T("玩家数(%1),iMyID(%2),房主(%3)")
            .arg(playerCount)
            .arg(iMyID)
            .arg(isMaster));
    //RefreshUI();
    //m_Player->getShouPai(m_PaiDui + 59);
    //m_Player->getShouPai(m_PaiDui + 60);
    //m_Player->getShouPai(m_PaiDui + 64);
    //m_Player->getShouPai(m_PaiDui + 65);
    //m_Player->getShouPai(m_PaiDui + 68);

    for(;;iMainTurn ++) //游戏主循环
	{
        srand();
		iMainTurn = iMainTurn % playerCount;

        Debug(_T("iMain(%1)").arg(iMainTurn));

        if(m_Player[iMainTurn].bDeadFlag == true)
            continue;
        if(m_Player[iMainTurn].bUsing == false)
            setGameStatus(Game_Status_Err);

        m_Player[iMainTurn].iSha = 1;
        m_Player[iMainTurn].iJieDuan = 0;
        m_Player[iMainTurn].iChuPaiPoint = 0;


        //阶段:
        while(m_Player[iMainTurn].iJieDuan < 5)
        {
            if(m_Player[iMainTurn].bDeadFlag == true)
                break;
            switch(m_Player[iMainTurn].iJieDuan)
            {
            case 0:
                setGameStatus(HuiHeKaiShi(iMainTurn));
                emit s_RefreshAllPokePosition();
                emit s_RefreshAllFlags();
                break;
            case 1:
                setGameStatus(PanDingJieDuan(iMainTurn));
                break;
            case 2:
                setGameStatus(MoPaiJieDuan(iMainTurn));
                break;
            case 3:
                setGameStatus(ChuPaiJieDuan(iMainTurn));
                break;
            case 4:
                setGameStatus(QiPaiJieDuan(iMainTurn));
                break;
            default:
                break;
            }
            if(eGameStatus != Game_Status_Continue)break;
            if(m_Player[iMainTurn].isComputer())
                Sleep(config.configHash["DELAY_TIME_COMPUTER"].toInt());
            m_Player[iMainTurn].iJieDuan++;
        }
        if(eGameStatus == Game_Status_Continue)
            setGameStatus(HuiHeJieShu(iMainTurn));
        else break;


        str._Format(("-------弃牌堆：%d---------"),QiPaiDuiShu);
        ShowMessage(str);

//		if(Huihe(iMainTurn) == -1)break;
		//if(Huihe(paidui,&paiduishu,qipaidui,&qipaishu,&com,&wanjia)==-1)break;
	}

    //if(iGameStatus == Game_Status_Gameover)
        ShowWinner();
    return eGameStatus;
}


int _GAME::ShowWinner()
{
    str = _T("游戏结束");
    ShowMessage(str);
	return 0;
}

void _GAME::CreatePaiDuiID()
{
    srand();

    diffarr(0,QiPaiDuiShu,PaiDuiID,QiPaiDuiShu);

    /*
    //新算法!!!???改!!!
    for(i=0; i<POKE_MAXNUM; i++)	//!!!将 a 中的每个1~POKE_MAXNUM定位到x,y,z
    {
        x=(a[i]-1)/52;
        y=((a[i]-1)-52*x)/13;
        z=(a[i]-1)%13;
        paidui[i]=A[x][y][z];		//x,y,z对应一个a
    }*/
}
void _GAME::RefreshPaiDui()
{
    int i;
    for(i = 0; i < QiPaiDuiShu; i++)	//按照PaiDuiID将QiPaiDui中的牌放入PaiDui
    {
        PaiDui[i] = QiPaiDui[PaiDuiID[i]];
        QiPaiDui[PaiDuiID[i]] = NULL;
        //PaiDui[i]->setFlag(BASE_PAI::flag_Show);
        //PaiDui[i]->moveTo(PAIDUI_X,PAIDUI_Y);
        PaiDui[i]->setPosition(BASE_PAI::Area_PaiDui,i,-1);
    }
    PaiDuiShu += QiPaiDuiShu;
    QiPaiDuiShu = 0;

    //剩下的牌
    for(i = PaiDuiShu; i < GAME_MAX_NUM_PAI; i++)
        PaiDui[i] = NULL;

}


void _GAME::Xipai()
{
    CreatePaiDuiID();
    RefreshPaiDui();
}

_Pai* _GAME::Mo1Pai()
{
    _Pai* p;
	if(PaiDuiShu == 0)
		Xipai();
	else if(PaiDuiShu < 0)
	{
        Debug("PaiDuiShu < 0");
		return NULL;
    }
    p = PaiDui[PaiDuiShu - 1];		//取牌
    PaiDuiShu--;
    //p->setPosition(ITEM_TYPE_NULL,-1,-1);
	return p;
}

void _GAME::MoPai(int w,int n)
{
    str = msgHash[_Pai::Value_All][2]
            .arg(getPlayerShowName(w))
            .arg(n);
    ShowMessage(str);

	//摸n牌
	int i;
	for(i = 0; i < n; i++)
    {
        _Pai *p = Mo1Pai();
        if(m_Player[w].getShouPai(p) < 0)
        {
            QiPai(p);
        }
    }
}

_GAME::_GameStatus _GAME::DiaoXie(int w,int x)
{
    str = msgHash[_Pai::Value_All][3]
        .arg(getPlayerShowName(w))
        .arg(x);
    ShowMessage(str);

    m_Player[w].iTili -= x;
    emit s_DiaoXie(w,m_Player[w].iTili);
    //RefreshUI();

    if(m_Player[w].iTili > 0 )
        return setGameStatus(Game_Status_Continue);
    else	//w濒死
    {
        int ret;
        int ww = iMainTurn;
        do
        {
            for(;;)	//向ww求桃
            {
                ret = Qiutao(w,ww);
                if(ret == Game_Button_OK)
                {
                    if( ChiTao(w) > 0 )
                        return setGameStatus(Game_Status_Continue);
                    continue;
                }
                else if(ret == Game_Button_Cancel)	//ww不出桃
                {
                    break;
                }
            }
        }
        while((ww = GetNextPlayer(ww)) != iMainTurn);
        return SetPlayerDead(w);

        /*
        if((*w1).juese)	//w1是玩家
        {
        for(;;)	//向w1求桃
        {
        printf("电脑濒死，是否使用%s？",PAI_103);
        i=Qiutao(w1,qipaidui,qipaishu);
        if(i==0)((*w2).tili)++;
        if((i==-1)||((*w2).tili>0))	//不出桃 或 体力大了
        break;
        }
        if((*w2).tili>0)
        return 0;
        else	//向w2求桃
        {
        for(;;)
        {
        i=Qiutao(w2,qipaidui,qipaishu);
        if(i==0)((*w2).tili)++;
        if((i==-1)||((*w2).tili>0))break;
        }
        if((*w2).tili>0)return 0;
        else return -1;
        }
        }
        else	//w1是电脑,w2玩家
        {
        for(;;)
        {
        printf("玩家濒死，是否使用%s？",PAI_103);
        i=Qiutao(w2,qipaidui,qipaishu);
        if(i==0)((*w2).tili)++;
        if((i==-1)||((*w2).tili>0))break;
        }
        if((*w2).tili>0)return 0;
        else return -1;
        }
        */
    }
}
_GAME::_GameButtonValue _GAME::Qiutao(int w1,int w2)
{
    _GameButtonValue ret;
    for(;;)
    {
        //cstr.Format(_T("如果要使用%s请输入手牌之前的编号，不需要请输入“-1”，以回车结束！"),PAI_103);

        if(m_Player[w2].isMe())
        {
            str = msgHash[_Pai::Value_Tao][2]
                .arg(getPlayerShowName(w1))
                .arg(_Pai::getName(_Pai::Value_Tao));
            ShowMessage(str);
        }

        ret = ChuPai(w2,_GAME::Game_ChuPai_Type_BeiDong, _Pai::Value_Tao);
        if(ret == Game_Button_Cancel)
        {
            str = msgHash[_Pai::Value_Tao][3]
                .arg(m_Player[w2].getWujiangData()->getName())
                .arg(_Pai::getName(_Pai::Value_Tao));
            ShowMessage(str);
            return ret;
        }

        else if(ret == Game_Button_OK)		//桃
        {
            str = msgHash[_Pai::Value_Tao][4]
                .arg(m_Player[w2].getWujiangData()->getName())
                .arg(_Pai::getName(_Pai::Value_Tao));
            ShowMessage(str);
            return Game_Button_OK;
        }
        else
        {
            str = _T("你确定你使用的是%1？")
                    .arg(_Pai::getName(_Pai::Value_Tao));
            ShowMessage(str);
        }
    }
}

//设置w死亡,弃牌,返回是否继续游戏(Game_Status_Gameover)
_GAME::_GameStatus _GAME::SetPlayerDead(int w)
{
    m_Player[w].bDeadFlag = true;
	
    str = msgHash[_Pai::Value_All][4]
            .arg(getPlayerShowName(w));
    ShowMessage(str);
    emit s_SetPlayerDead(w);
	
	// +  + 
	int i;
    for(i = 0; i < m_Player[w].ShouPaiList.count(); i++)
	{	
        _Pai *p = m_Player[w].qiShouPai(i,false);
        if(p != NULL)
		{
            str = msgHash[_Pai::Value_All][5]
                    .arg(getPlayerShowName(w))
                    .arg(p->getName());
            ShowMessage(str);
			QiPai(p);
        }
	}
    m_Player[w].tiaoZhengShouPaiDui();

    while(1)
	{	
        _Pai *p = m_Player[w].qiZhuangBei();
        if(p != NULL)
		{
            str = msgHash[_Pai::Value_All][5]
                .arg(getPlayerShowName(w))
                .arg(p->getName());
            ShowMessage(str);
			QiPai(p);
		}
        else
            break;
	}
    while(m_Player[w].PanDingList.count() > 0)
	{	
        _Pai *p = m_Player[w].qiPanDing(0);
        if(p != NULL)
		{
            str = _T("%1弃牌:%2")
                    .arg(getPlayerShowName(w))
                    .arg(p->getName());
            ShowMessage(str);
			QiPai(p);
		}
	}
    //m_Player[w].ZhuangBeiCount = 0;//m_Player[w].PanDingCount = m_Player[w].ShouPaiCount = 0;
	/*
	for(i = 0; i < GAME_PLAYER_ZHUANGBEISHU; i++)
	{
		if(m_Player[w].zhuangbei[i].pData == NULL)
			continue;
		
		cstr.Format(_T("%s弃牌:%s"),
			m_Player[w].strName,m_Player[w].zhuangbei[i]);
		ShowMessage(cstr);
		QiPai(m_Player[w].zhuangbei[i].QiPai());
		Player[w].zhuangbei[i].iValue = GAME_POKE_EMPTY;
	}
	
	for(i = 0; i < GAME_PLAYER_PANDINGSHU; i++)
	{
		if( m_Player[w].pandingpai[i].leixing == GAME_POKE_EMPTY)
			continue;
		cstr.Format(_T("%s弃牌:%s"),
			Player[w].NameString(),Player[w].pandingpai[i].PaiString());
		ShowMessage(cstr);
		
		QiPai(Player[w].pandingpai[i]);
		Player[w].pandingpai[i].iValue = GAME_POKE_EMPTY;
    }
    if(w == iMainTurn)
        setGameStatus(Game_Status_NextTurn);
    */
	
	//检测游戏是否结束
	return CheckGameOver();
}

_GAME::_GameStatus _GAME::CheckGameOver()
{
    if(eGameStatus != Game_Status_Continue)
    {
        //exit(0);
        return eGameStatus;
    }

    int n = 0, i;
    for(i = 0; i < playerCount; i++)
	{
        if(m_Player[i].bDeadFlag == false)
			n++;
	}
	if(n == 1)
    {
        //exit(0);
        return setGameStatus(Game_Status_GameOver);
    }
    else
        return eGameStatus;
    /*
        if(eGameStatus == Game_Status_NextTurn)
            return Game_Status_NextTurn;
        else
            return setGameStatus(Game_Status_Continue);
            */
}

int _GAME::ShaiDian()
{
    return Random(1,7);
}

_GAME::_GameStatus _GAME::HuiHeKaiShi(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].huihekaishi)
	{
	case 0:
        str = _T("%1回合开始阶段……")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
		break;
	default:
        ShowMessage(_T("回合开始阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
    }
    return setGameStatus(Game_Status_Continue);
}

_GAME::_GameStatus _GAME::PanDingJieDuan(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].pandingjieduan)
	{
	case 0:
        str = _T("%1判定阶段……")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
        setGameStatus(PanDingChuLi(w));
        if(eGameStatus != Game_Status_Continue)
            return eGameStatus;
		break;
	case -1:
		break;
	default:
		ShowMessage(_T("判定阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
	}

	//else if(Player[w].PanDingShu() == 0);
	//else ShowMessage(_T("判定阶段参数错误！"));

    return setGameStatus(Game_Status_Continue);
}

_GAME::_GameStatus _GAME::MoPaiJieDuan(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].mopaijieduan)
	{
	case 0:
        str = _T("%1摸牌阶段……")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
        setGameStatus(MoPaiChuLi(w));
        if(eGameStatus != Game_Status_Continue)
            return eGameStatus;
		break;
	case -1:
		break;
	default:
		ShowMessage(_T("摸牌阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
    }

    return setGameStatus(Game_Status_Continue);
	
}

_GAME::_GameStatus _GAME::ChuPaiJieDuan(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].chupaijieduan)	//出牌 为0可以出牌,-1不可以出牌
	{
	case 0:
        str = _T("%1出牌阶段")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
        setGameStatus(ChuPaiChuLi(w));
        if(eGameStatus != Game_Status_Continue)
            return eGameStatus;
	case -1:
        m_Player[w].chupaijieduan = 0;
		break;
	default:
		ShowMessage(_T("出牌阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
    }

    return setGameStatus(Game_Status_Continue);
}

_GAME::_GameStatus _GAME::QiPaiJieDuan(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].qipaijieduan)
	{
	case 0:
        str = _T("%1弃牌阶段……")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
        setGameStatus(QiPaiChuLi(w));
        if(eGameStatus != Game_Status_Continue)
		{	
            return eGameStatus;
		}
		break;
	default:
		ShowMessage(_T("弃牌阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
    }

    return setGameStatus(Game_Status_Continue);
	
}


_GAME::_GameStatus _GAME::HuiHeJieShu(int w)
{
    if(m_Player[w].bDeadFlag)
		return CheckGameOver();

    switch(m_Player[w].huihejieshujieduan)
	{
	case 0:
        str = _T("%1回合结束阶段……")
                .arg(getPlayerShowName(w));
        ShowMessage(str);
        ShowChuPaiMessage(str);
		break;
	default:
		ShowMessage(_T("回合结束阶段参数错误！"));
        return setGameStatus(Game_Status_GameOver);
		break;
	}

    return setGameStatus(Game_Status_Continue);
}


int _GAME::QiPai(_Pai *p)
{
    if(p != NULL)
    {
        QiPaiDui[QiPaiDuiShu] = p;
        //p->setFlag(_Pai::flag_ZhengMian & _Pai::flag_Show);
        p->setPosition(BASE_PAI::Area_QiPaiDui,QiPaiDuiShu,-1);
        QiPaiDuiShu++;
	}
	else
	{
        Debug("!!!弃牌1Err!!!");
		return -1;
	}
	if(QiPaiDuiShu > GAME_MAX_NUM_PAI)
        Debug("!!!弃牌2Err!!!");

	return QiPaiDuiShu;
}

int _GAME::GetNextPlayer(int ww)
{
    //int ww = w;
	for(;;)
	{
		ww ++;
        if(ww < 0)ww = 0;
		ww = ww % playerCount;
        if(m_Player[ww].bDeadFlag == false)
			return ww;
	}
}

void _GAME::FlagMenus(BASE_PAI::_Flags ok, BASE_PAI::_Flags cancel, BASE_PAI::_Flags giveup)
{
    Button[Game_Button_OK] = ok;
    Button[Game_Button_Cancel] = cancel;
    Button[Game_Button_Giveup] = giveup;
    //qDebug()<<"C flag Menus!!!"<<ok<<cancel<<giveup;
    //QVariant vOK,vCancel,vGiveup;
    //vOK = QVariant::fromValue(ok);
    //vCancel = QVariant::fromValue(cancel);
    //vGiveup = QVariant::fromValue(giveup);
    //emit s_FlagMenus2(QVariant::fromValue(ok),QVariant::fromValue(cancel),QVariant::fromValue(giveup));
    //emit s_FlagMenus1(ok,cancel,giveup);
    emit s_FlagMenus(ok,cancel,giveup);
}

QList<_Player*> _GAME::resetAllPlayers(QList<_Player*> list)
{
    //int nCount = 0;
    QList<_Player*>::const_iterator ii;
    for (ii = list.constBegin(); ii != list.constEnd(); ++ii)
    {
        (*ii)->resetAllFlags();
        //nCount ++;
    }
    return list;

}

QList<_Player*> _GAME::getAllPlayers(int type)
{
    int i;
    QList<_Player*> l;
    for(i = 0; i < GAME_MAX_NUM_PLAYER; i++)	//清除武将数
    {
        if(type == 0 && !m_Player[i].bDeadFlag && m_Player[i].bUsing)
        {
            l.append(m_Player + i);
        }
    }
    return l;
}

QList<_Player*> _GAME::removePlayer(QList<_Player*> list, int id)
{
    int i;
    for(i = 0; i < list.length(); i++)	//清除武将数
    {
        if(list.value(i)->id() == id)
        {
            list.removeAt(i);
            //return list;
        }
    }
    return list;
}

QList<_Player*> _GAME::removePlayer(QList<_Player*> list, BASE_PAI::_Flags flags)
{
    int i;
    for(i = 0; i < list.length(); i++)	//清除武将数
    {
        if(list.value(i)->flag() & flags)
        {
            list.removeAt(i);
        }
    }
    return list;
}

QList<_Player*> _GAME::retainPlayer(QList<_Player*> list, BASE_PAI::_Flags flags)
{
    int i;
    for(i = 0; i < list.length(); i++)	//清除武将数
    {
        if(!(list.value(i)->flag() & flags))
        {
            list.removeAt(i);
        }
    }
    return list;
}

QList<_Player*> _GAME::flagAllPlayers(QList<_Player*> list, BASE_PAI::_Flags eFlags, BASE_PAI::_FlagType type)
{

    //int nCount = 0;
    QList<_Player*>::const_iterator ii;
    for (ii = list.constBegin(); ii != list.constEnd(); ++ii)
    {
        (*ii)->setFlag(eFlags,type);
        //nCount ++;
    }
    return list;
}

int _GAME::flagAllPlayersByPanDing(QList<_Player*> list, BASE_PAI::_Flags flags, _Pai::_PaiValue pandingValue)
{

    //int nCount = 0;
    QList<_Player*>::const_iterator ii;
    for (ii = list.constBegin(); ii != list.constEnd(); ++ii)
    {
        if((*ii)->getPaiCount(pandingValue, BASE_PAI::Area_PanDing) > 0)
        {
            //if(!test)
            (*ii)->setFlag(flags);
            //nCount ++;

        }
    }
    return list;

}

int _GAME::flagAllPlayersByPaiCount(QList<_Player*> list, BASE_PAI::_Flags flags, int count)
{
    //int nCount = 0;
    QList<_Player*>::const_iterator ii;
    for (ii = list.constBegin(); ii != list.constEnd(); ++ii)
    {
        if((*ii)->getPaiCount() >= count)
        {
            m_Player[i].setFlag(flags);
            //nCount ++;
        }
    }
    return list;
}

int _GAME::FlagAllPlayersWeapon(BASE_PAI::_Flags eFlags, BASE_PAI::_Areas areas, BASE_PAI::_FlagType type, int player)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
        return -1;

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置w为完成标记
    do
    {
        nCount += m_Player[i].flagZhuangBeiByArea(eFlags,areas,type);
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}


int _GAME::EnableAllPlayersByDistent(int player, int type, bool test, int ignoreEnable)
{
    int nCount = 0;

    if(!CheckPlayerPlay(player,false))
        return -1;

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        if(ignoreEnable == 1 && (m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            continue;
        }

        if(ComputeDistent(player,i,type))
        {
            if(!test)m_Player[i].setAFlag(BASE_PAI::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_Player[i].clearAFlag(BASE_PAI::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}


int _GAME::EnableAllPlayersByZhuangBei(int player, _Pai::_PaiValue value, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
        return -1;

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_Player[i].bDeadFlag == true)continue;
        //if(m_Player[i].getZhuangBeiData(BASE_PAI::Area_WuQi) == NULL)continue;
        if(ignoreEnable == 1 && (m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            continue;
        }
        if(m_Player[i].containZhuangBei(value))
        {
            if(!test)m_Player[i].setAFlag(BASE_PAI::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_Player[i].clearAFlag(BASE_PAI::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int _GAME::EnableAllPlayersByPanDing(int player, _Pai::_PaiValue value, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
        return -1;

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_Player[i].bDeadFlag == true)continue;
        if(ignoreEnable == 1 && (m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            continue;
        }
        if(m_Player[i].containPanDing(value))
        {
            if(!test)m_Player[i].setAFlag(BASE_PAI::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_Player[i].clearAFlag(BASE_PAI::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

int _GAME::EnableAllPlayersByShouPai(int player, bool test, int ignoreEnable)
{
    int nCount = 0;
    if(!CheckPlayerPlay(player,true))
        return -1;

    int i = GetNextPlayer(player);if(player == -1)player = i;   //设置player为完成标记
    do
    {
        //if(m_Player[i].bDeadFlag == true)continue;
        if(ignoreEnable == 1 && m_Player[i].flag() & BASE_PAI::Flag_Enable)
        {
            nCount ++;
            continue;
        }
        if(ignoreEnable == 2 && !(m_Player[i].flag() & BASE_PAI::Flag_Enable))
        {
            continue;
        }
        if(m_Player[i].getPaiCount(_Pai::Value_All,BASE_PAI::Area_ShouPai) > 0)
        {
            if(!test)m_Player[i].setAFlag(BASE_PAI::Flag_Enable);
            nCount ++;
        }
        else
        {
            if(!test)m_Player[i].clearAFlag(BASE_PAI::Flag_Enable);
        }
    }
    while(i = GetNextPlayer(i),i != player);

    return nCount;
}

void _GAME::run()
{
    /* 线程的相关代码 */
    //pGame->m_pGameRes->m_WuJiang[0].setPixmap(QPixmap(":/Images/Pai/Sha"));

    //m_mutex.tryLock();      //不加这句就会出错!不知道为何
    //for(int i = 0; i < GAME_MAX_NUM_PAI; i++)
    //    Debug(_T(PaiDuiID[i]));
    Init();
    GameBegin();
}


//////////////////////Game/////////////////////////////////////

Game::Game(QObject *parent)
    :_GAME(parent)
{
    //m_mutexNet.tryLock();




    //!!加牌!!
    /*
    for(n = GAME_MAX_NUM_PAI; i < n; i++)   //桃
    {
        m_PaiDui[i].setProp(i,POKE_TYPE_JIBEN,_Pai::Value_Tao);
        m_PaiDui[i].pai = &pai_Tao;
    }*/

//    for(n += 30;i < n;i++)  //杀
//    {
//        m_PaiDui[i].setProp(i,POKE_TYPE_JIBEN,_Pai::Value_Sha);
//        m_PaiDui[i].pai = &pai_Sha;
//    }
//    for(n = GAME_MAX_NUM_PAI;i < n;i++)   //碳素笔
//    {
//        m_PaiDui[i].setProp(i,POKE_TYPE_ZHUANGBEI,_Pai::Value_TanSu);
//        m_PaiDui[i].pai = &pai_TanSu;
//    }

    /*
    for(n+=10;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=4;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha309;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        m_pGameRes[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=6;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=8;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=1;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=3;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;
    for(n+=2;i<n;i++)
        yuanshipaidui[i].iValue=_Pai::Value_Sha;



    m_WuJiang[0].iType = _Pai::Type_WuJiang;
    m_WuJiang[0].iValue = VALUE_WUJIANG_LIUSHUAI;
    m_WuJiang[0].tilishangxian = 4;
    m_WuJiang[1].iType = _Pai::Type_WuJiang;
    m_WuJiang[1].iValue = VALUE_WUJIANG_SUM;
    m_WuJiang[1].tilishangxian = 4;
    / *
    pai_Sha.setProp(POKE_TYPE_JIBEN,_Pai::Value_Sha,"Sha.bmp");
    pai_Shan.setProp(POKE_TYPE_JIBEN,_Pai::Value_Shan,"Shan.bmp");
    pai_Tao.setProp(POKE_TYPE_JIBEN,_Pai::Value_Tao,"Tao.bmp");
    pai_ShanDian.setProp(POKE_TYPE_JINNANG1,_Pai::Value_ShanDian,"ShanDian.bmp");
    pai_Shun.setProp(POKE_TYPE_JINNANG0,_Pai::Value_Shun,"Shun.bmp");
    pai_Chai.setProp(POKE_TYPE_JINNANG0,_Pai::Value_Chai,"Chai.bmp");

    pai_ZhangBa.setProp(POKE_TYPE_WUQI,_Pai::Value_ZhangBa,"ZhangBa.bmp");
    pai_QingLong.setProp(POKE_TYPE_WUQI,_Pai::Value_QingLong,"QingLong.bmp");

    Button_OK.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_OK.bmp");
    Button_Cancel.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_Cancel.bmp");
    Button_Giveup.setProp(POKE_TYPE_NULL,POKE_TYPE_NULL,"Button_Giveup.bmp");
    */



}
int Game::Init()
{
    _GAME::Init();
    socket.clearData();
    //netPlay = false;
    //m_mutex.tryLock();

    qDeleteAll(dataList);
    dataList.clear();

    m_mutexDataList.tryLock();
    m_mutexDataList.unlock();

    /*
    while(!dataList.isEmpty())
    {
        delete dataList.takeFirst();
    }*/
    return 0;
}
/*
int Game::RefreshUI()
{
    //武将
    //int i;

    emit s_RefreshUI();

    return 0;
}
*/

_GameData * Game::WaitingForOthers()
{
    QList<_GameData*> *pDataList = getDataList();
    if(pDataList->count() == 0)
    {
        releaseDataList();
        Debug(_T("Waiting..."));
        //RefreshUI();
        WaitForNet();
        getDataList();
    }


    _GameData *pData = pDataList->takeFirst();
    releaseDataList();
    return pData;
/*
    if(pData->type == GAME_DATA_TRANSFER_XIPAI)
    {
        memcpy(PaiDuiID,pData->ChuPaiData.data,pData->ChuPaiData.dataLength);
        RefreshPaiDui();
        PaiDuiShu = pData->ChuPaiData.iData;

        delete pData;
        return 0;
    }
    else if(pData->type == GAME_DATA_TRANSFER_CHUPAI)
    {
        memcpy(step,pData->ChuPaiData.data,pData->ChuPaiData.dataLength);
        int ret = pData->ChuPaiData.iData;
        delete pData;

        return ret;
    }
    else
    {
        qDebug()<<"WaitingForOthers Err!"<<pData->type;
        return -1;
    }
    return -1;
    */
}

/*
void Game::SendToOthers(int iType)
{
    switch(iType)
    {
    case GAME_DATA_TRANSFER_XIPAI:
        {
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_XIPAI
                            <<(qint16)PaiDuiShu
                            <<(qint16)(sizeof(PaiDuiID));
            socket.sendData().writeRawData((char*)PaiDuiID,sizeof(PaiDuiID));
        }
        break;
    }
    emit s_SendToOthers();
}
*/

void Game::Xipai()
{
    if(isMaster)
    {
        _GAME::Xipai();
        if(netPlay == true)
        {
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_XIPAI
                            <<(qint16)PaiDuiShu
                            <<(qint16)(sizeof(PaiDuiID));
            socket.sendData().writeRawData((char*)PaiDuiID,sizeof(PaiDuiID));
            emit s_SendToOthers();
            Debug(_T("洗牌发送数据:"));
        }
    }
    else
    {
        Debug(_T("等待洗牌:"));

        _GameData *pData = WaitingForOthers();
        if(pData->type != GAME_DATA_TRANSFER_XIPAI)
        {
            Debug(_T("XiPai同步Err!"));
        }
        memcpy(PaiDuiID,pData->ChuPaiData.data,pData->ChuPaiData.dataLength);
        RefreshPaiDui();
        PaiDuiShu = pData->ChuPaiData.iData;

        delete pData;

        Debug(_T("洗牌接受ok"));
    }
}
Game::_GameButtonValue Game::ChuPai(int w,_GameChuPaiType nType, int nValue, int nParam)
{
    _GameButtonValue ret = Game_Button_Err;

    startChuPai(w);
    if(m_Player[w].isMe() || (isMaster == true && m_Player[w].isComputer()))
    {
        ret = _GAME::ChuPai(w,nType,nValue,nParam);
        ShowChuPaiMessage(_T(""));

        /*
        //拆和顺，随机选择一张牌!!!!!!
        if(step[0].nSelectType[0] == BASE_PAI::Area_ShouPai)
        switch(m_Player[w].getShouPaiData(step[0].nSelectValue[0])->value())
        {
        case _Pai::Value_Chai:
        case _Pai::Value_Shun:
            {
                //step[2].nSelectType[0] = BASE_PAI::Area_ShouPai;//!!!!!!
                //step[2].nSelectValue[0] = Random(0,m_Player[step[1].nSelectPlayerID].ShouPaiList.count());
            }
            break;
        default:
            break;
        }*/

        if(netPlay == true)
        {
            //本人出牌,发送数据
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_CHUPAI
                            <<(qint16)ret<<(qint16)sizeof(step);
            socket.sendData().writeRawData((char*)step,sizeof(step));
            emit s_SendToOthers();
        }
    }
    else    //是自己出牌 或 电脑出牌且本人是主机
    //(m_Player[w].IsOtherPlayer() || (isMaster == false && m_Player[w].IsComputer()))
    {
        ShowChuPaiMessage(_T("等待对方出牌..."));
        _GameData *pData = WaitingForOthers();
        ShowChuPaiMessage(_T(""));
        if(pData->type != GAME_DATA_TRANSFER_CHUPAI)
        {
            Debug(_T("ChuPai同步Err!"));
        }

        memcpy(step,pData->ChuPaiData.data,pData->ChuPaiData.dataLength);
        ret = (_GameButtonValue)pData->ChuPaiData.iData;
        delete pData;

    }
    stopChuPai(w);
    return ret;
}


//?????这里的s_Wenti有点问题!
bool Game::WenTi(int w)
{
    //return true;
    _EnglishWord *pWord = NULL;

    if(m_Player[w].isMe() || (isMaster == true && m_Player[w].isComputer()))
    {
        pWord = wordLib->createNewWord();
        wordLib->appendWord(pWord);
        wordObj.pWord = pWord;

        if(netPlay == true)
        {
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_WENTI
                            <<pWord->answer
                            <<pWord->userAnswer
                            <<pWord->word
                            <<pWord->phonetic
                            <<pWord->explain[0]
                            <<pWord->explain[1]
                            <<pWord->explain[2]
                            <<pWord->explain[3];

            emit s_SendToOthers();
        }

        if(m_Player[w].isMe())
        {
            ShowChuPaiMessage(str + _T("，请选择一个正确答案 "));
            emit s_Wenti(&wordObj,-2);
            //RefreshUI();
            WaitForQuestion();
        }
        else    //COM
        {
            str = str + _T("，%1正在选择答案")
                    .arg(getPlayerShowName(w));
            ShowChuPaiMessage(str);

            emit s_Wenti(&wordObj,-1);
            WaitForQuestion();
            //Sleep(5000);
            if(qrand() % 2 == 0)
                pWord->userAnswer = pWord->answer;
            else
                pWord->userAnswer = qrand() % 4;

        }
        answer = (pWord->answer == pWord->userAnswer);
        Debug(tr("选择第%1个答案").arg(pWord->userAnswer));

        if(netPlay == true)
        {
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_ANSWER
                            <<(qint16)(pWord->userAnswer);

            emit s_SendToOthers();
        }

        //if(m_Player[w].IsMe())
        {
            emit s_Wenti(&wordObj,pWord->userAnswer);
            //if(!m_Player[w].isMe())
            //    WaitForQuestion();
            //RefreshUI();
            WaitForQuestion();
        }
    }
    else
    {
        _GameData *pData = WaitingForOthers();
        if(pData->type != GAME_DATA_TRANSFER_WENTI)
        {
            Debug(_T("问题同步Err!"));
        }
        pWord = pData->WenTiData;
        wordLib->appendWord(pWord);

        delete pData;

        str = str + _T("，%1正在选择答案")
                .arg(getPlayerShowName(w));
        ShowChuPaiMessage(str);
        emit s_Wenti(&wordObj,-1);
        //RefreshUI();
        //WaitForUI();



        pData = WaitingForOthers();
        if(pData->type != GAME_DATA_TRANSFER_ANSWER)
        {
            Debug(_T("问题同步Err!"));
        }
        pWord->userAnswer = pData->iAnswer;

        delete pData;
        emit s_Wenti(&wordObj,pWord->userAnswer);
        WaitForQuestion();
        //RefreshUI();
        //WaitForQuestion();

        answer = (pWord->userAnswer == pWord->answer);
    }

    str = _T("[提问]%1:%2\r\n[回答]")
            .arg(getPlayerShowName(w))
            .arg(pWord->word);
    if(pWord->userAnswer == -1)
        str.append(_T("不知道"));
    else
        str.append(pWord->explain[pWord->userAnswer]);
    ShowMessage(str);

    return answer;
}

int Game::ShaiDian()
{
    qint32 n;
    if(isMaster)
    {
        n = _GAME::ShaiDian();

        if(netPlay == true)
        {
            socket.sendData()<<(qint16)GAME_DATA<<(qint16)GAME_DATA_TRANSFER_RANDOM<<n;
            emit s_SendToOthers();
            Debug(_T("骰点发送数据:"));
        }
    }
    else
    {
        Debug(_T("等待骰点:"));

        _GameData *pData = WaitingForOthers();
        if(pData->type != GAME_DATA_TRANSFER_RANDOM)
        {
            Debug(_T("ShaiDian同步Err!"));
        }
        n = pData->iRandom;
        delete pData;

        Debug(_T("骰点接受ok"));
    }

    //n = n % 6;
    //ShowMessage(_T("筛到%1点!").arg(n));

    return n;
}


void Game::buttonClicked(int button)
{
    if(button ==  -1)
    {
        if(Button[Game_Button_Giveup] ==  BASE_PAI::Flag_Enable)
        {
            dianjipai.nPlayerID = -1;
            dianjipai.nType = BASE_PAI::Area_Button;
            dianjipai.nValue = Game_Button_Giveup;
            dianjipai.iFlag = Button[Game_Button_Giveup];
            dianjipai.pObject = NULL;
        }
        else if(Button[Game_Button_Cancel] ==  BASE_PAI::Flag_Enable)
        {
            dianjipai.nPlayerID = -1;
            dianjipai.nType = BASE_PAI::Area_Button;
            dianjipai.nValue = Game_Button_Cancel;
            dianjipai.iFlag = Button[Game_Button_Cancel];
            dianjipai.pObject = NULL;
        }
        else
            Debug("TimeUp Err!!!");

        //qDebug()<<this->iType<<","<<this->iPositionPlayerID<<",ID:"<<this->iID << "zvalue:" << zValue();
        //if(event->button() == Qt::LeftButton)
        //    QMessageBox::information(NULL,"aa","aa");
        //emit s_ClickPai(dianjipai);
        m_waitForChoice.wakeAll();
        return;
    }

    //if(mouseButton == Qt::LeftButton && (this->iFlag & GAME_POKE_FLAG_ENABLE))
    if(Button[button] ==  BASE_PAI::Flag_Enable)
    {
        dianjipai.nPlayerID = -1;
        dianjipai.nType = BASE_PAI::Area_Button;
        //if(dianjipai.nType == ITEM_TYPE_WUJIANG)
            dianjipai.nValue = button;
        //else
            //dianjipai.nValue = m_PaiDui[n].positionValue();
            dianjipai.iFlag = Button[button];
            dianjipai.pObject = NULL;

        //qDebug()<<this->iType<<","<<this->iPositionPlayerID<<",ID:"<<this->iID << "zvalue:" << zValue();
        //if(event->button() == Qt::LeftButton)
        //    QMessageBox::information(NULL,"aa","aa");
        //emit s_ClickPai(dianjipai);
        m_waitForChoice.wakeAll();
    }
}

void Game::debugButton(int button, QVariant v)
{
    if(button == 10)
    {
        Debug(_T("牌堆:"));
        for(int i = 0; i < PaiDuiShu; i++)
        {
            Debug(_T("%1(%2),").arg(PaiDui[i]->id()).arg(PaiDui[i]->getName()));
        }
        return;
    }
    else if(button == 11)
    {
        Debug(_T("弃牌堆:"));
        for(int i = 0; i < QiPaiDuiShu; i++)
        {
            Debug(_T("%1(%2),").arg(QiPaiDui[i]->id()).arg(QiPaiDui[i]->getName()));
        }
        return;
    }
    else if(button == 12)
    {
        Debug(_T("Player手牌"));
        for(int i = 0; i < m_Player[v.toInt()].ShouPaiList.length(); i++)
        {
            Debug(_T("%1(%2),").arg(((_Pai*)m_Player[v.toInt()].ShouPaiList[i])->id()).arg(((_Pai*)m_Player[v.toInt()].ShouPaiList[i])->getName()));
        }
        Debug(_T("Player装备"));
        for(int i = 0; i < m_Player[v.toInt()].ZhuangBeiList.length(); i++)
        {
            Debug(_T("%1(%2),").arg(((_Pai*)m_Player[v.toInt()].ZhuangBeiList[i])->id()).arg(((_Pai*)m_Player[v.toInt()].ZhuangBeiList[i])->getName()));
        }
        Debug(_T("Player判定"));
        for(int i = 0; i < m_Player[v.toInt()].PanDingList.length(); i++)
        {
            Debug(_T("%1(%2),").arg(((_Pai*)m_Player[v.toInt()].PanDingList[i])->id()).arg(((_Pai*)m_Player[v.toInt()].PanDingList[i])->getName()));
        }
        return;
    }
    else if(button == 14)
    {
        emit s_RefreshAllFlags();
        return;
    }

    else if(button == 15)
    {
        //Debug(_T("牌堆:"));
        int i;
        for(i = 0; i < PaiDuiShu; i++)
        {
            if(v.toInt() == PaiDui[i]->value())
            {
                m_Player[iMyID].getShouPai(PaiDui[i]);
                for(; i < PaiDuiShu; i++)
                {
                    PaiDui[i] = PaiDui[i + 1];
                }
                PaiDuiShu--;
                break;
            }
        }
        for(i = 0; i < QiPaiDuiShu; i++)
        {
            if(v.toInt() == QiPaiDui[i]->value())
            {
                m_Player[iMyID].getShouPai(QiPaiDui[i]);
                for(; i < QiPaiDuiShu; i++)
                {
                    QiPaiDui[i] = QiPaiDui[i + 1];
                }
                QiPaiDuiShu--;
                break;
            }
        }
        emit s_RefreshAllPokePosition();
        emit s_RefreshAllFlags();

    }
    return;
}
